﻿@model System.Data.DataTable           
@using App.Web.Utility;

@{
    String pageTitle = "Manage User";
    ViewBag.Title = Html.GetPageTitle(pageTitle);
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<div class="page-heading">
    <h1>@pageTitle</h1>
    <div>
        @if (UserInfo.IsCompanyAdmin || UserInfo.IsAdmin)
        {
            @:Users of your company. To create new user go to @Html.PrivateLink("Manage Contacts", "List", "Contact") and select the contact you want to make user.
        }
        else
        {
            @:Users of your company.
        }
    </div>
</div>
<div class="GroupBox">
    @*<a href="/Private/Contact/Edit">Add New Contact</a><br /><br />*@
    @Html.Partial("_ListView", ViewBag.ListConfig as App.Web.ViewModels.UtilityModels.ListConfig)

    <div id="divCreateUserDialog" style="display:none;">
    <form id="frmCreateUser" method="post" action="/Private/Contact/CreateUser">
        <div class="editor-label">
            <label for="Email">User Name</label>
        </div>
        <div class="editor-field">
            
            <input
                data-val="true" 
                data-val-required="Please enter an User Name"
                data-val-length="The User Name must be at least 6 - 10 characters long." 
                data-val-length-max="10" 
                data-val-length-min="6"
                id="UserID" name="UserID" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="UserID" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="Password">Password</label>
        </div>
        <div class="editor-field">
            <input
                data-val="true" 
                data-val-length="The Password must be at least 6 characters long." 
                data-val-length-max="100" 
                data-val-length-min="6" 
                data-val-required="Please Enter Password."              
                class="text-box single-line password" id="Password" name="Password" type="password" value="" />            
            <span class="field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="ConfirmPassword">Confirm password</label>
        </div>
        <div class="editor-field">
            <input
                data-val="true" 
                data-val-equalto="The password and Confirm password do not match." 
                data-val-required="Please Confirm your Password."
                data-val-equalto-other="*.Password"
                id="ConfirmPassword" name="ConfirmPassword" type="password" value="" />
            <span class="field-validation-valid" data-valmsg-for="ConfirmPassword" data-valmsg-replace="true"></span>
        </div>
        <div id="divSuccessMessage" style="display:none;"></div>        
    </form>
    </div>
</div>
<script language="javascript" type="text/javascript">

    var _ContactID = 0;
    var _SaveEdit = "";
    var _CategoryDialog = null;
    $(document).ready(function () {
        _CategoryDialog = $('#divCreateUserDialog').dialog({
            autoOpen: false,
            title: 'Create User',
            modal: true,
            width: 260,
            buttons: {
                'Save': function () { SaveUser(); }
                //, 'Cancel': function () { $(this).dialog('close'); }
            }
        });        
    });
    function MakeUser(contactID, saveEdit) {
        //alert(CreateEdit);

        _ContactID = contactID;
        _SaveEdit = saveEdit;

        $('#UserID').val(saveEdit);
        $('#Password').val('');
        $('#ConfirmPassword').val('');

        //$('#divCreateUserDialog').show();
        //displayDiv = document.getElementById("divCreateUserDialog");
        //displayDiv.style.display = "block";
        $('#divSuccessMessage').html('').hide();
        $(_CategoryDialog).dialog('open'); //alert($(this).val());
    }

    function SaveUser() {        
        if ($('#frmCreateUser').valid()) {
            $.ajax({
                type: 'POST',
                url: '@Url.Action("CreateUser", "Contact", new {area=AppConstants.Area.PRIVATE})',
                data: 'UserName=' + $('#UserID').val() + '&password=' + $('#Password').val() + '&contactId=' + _ContactID + '&saveEdit=' + _SaveEdit,
                success: function (result) {
                    if (result == -1) {
                        $('#divSuccessMessage').removeClass('@AppConstants.UI.ERROR_MESSAGE_CLASS @AppConstants.UI.MESSAGE_BOX_CLASS').addClass('@AppConstants.UI.ERROR_MESSAGE_CLASS').html('Sorry! the User Name already exists, please choose another.').show();
                    }                    
                    else if (result == 1) {
                        $('#divSuccessMessage').removeClass('@AppConstants.UI.ERROR_MESSAGE_CLASS @AppConstants.UI.MESSAGE_BOX_CLASS').addClass('@AppConstants.UI.MESSAGE_BOX_CLASS').html('Congratulations! User saved successfully.').show();
                        //window.location.href = "/Private/Contact/List";
                        ///Update the Corresponding Table Cell with the Appropriate values rather than reloading the page.
                    }
                }
            });
        }
    }
</script>

